package org.jsola.hr.web;

import org.apache.commons.io.FilenameUtils;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.MissionCenterExportWrongDTO;
import org.jsola.hr.dto.MissionCenterFeedBackDTO;
import org.jsola.hr.dto.MissionCenterItemAddDTO;
import org.jsola.hr.dto.MissionCenterItemUpdateDTO;
import org.jsola.hr.exception.HrException;
import org.jsola.hr.query.MissionCenterItemQuery;
import org.jsola.hr.service.IMissionCenterItemService;
import org.jsola.hr.vo.MissionCenterItemListVO;
import org.jsola.hr.vo.MissionCenterItemVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.StandardCharsets;
import java.util.List;
import javax.validation.Valid;


/**
 * 任务调度中心任务子项表
 *
 * @author zhr
 */
@RestController("hrMissionCenterItemController")
@RequestMapping(HrConstants.API_PATH + "/v1/missionCenterItems")
public class MissionCenterItemController implements IMissionCenterItemControllerAPI {

    @Autowired
    private IMissionCenterItemService missionCenterItemService;

    @Override
    @PostMapping
    public Result<MissionCenterItemVO> save(@Valid @RequestBody MissionCenterItemAddDTO missionCenterItemAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增任务调度中心任务子项表权限校验

        return Result.success(
                missionCenterItemService.save(missionCenterItemAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody MissionCenterItemUpdateDTO missionCenterItemUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改任务调度中心任务子项表权限校验

        int count = missionCenterItemService.update(missionCenterItemUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @DeleteMapping(value = "/{missionCenterItemId}")
    public Result<Integer> delete(@PathVariable String missionCenterItemId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除任务调度中心任务子项表权限校验

        int count = missionCenterItemService.deleteByIds(tokenUser, missionCenterItemId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] missionCenterItemIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除任务调度中心任务子项表权限校验

        int count = missionCenterItemService.deleteByIds(tokenUser, missionCenterItemIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{missionCenterItemId}")
    public Result<MissionCenterItemVO> findById(@PathVariable String missionCenterItemId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看任务调度中心任务子项表权限校验

        return Result.success(
                missionCenterItemService.selectById(missionCenterItemId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<MissionCenterItemListVO>> listAll(@Valid MissionCenterItemQuery missionCenterItemQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看任务调度中心任务子项表权限校验

        return Result.success(
                missionCenterItemService.select(missionCenterItemQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<MissionCenterItemListVO>> page(@Valid MissionCenterItemQuery missionCenterItemQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                missionCenterItemService.selectPage(missionCenterItemQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/selectMissionCenterItem")
    public Result<Page<MissionCenterItemListVO>> selectMissionCenterItem(@Valid MissionCenterItemQuery missionCenterItemQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                missionCenterItemService.selectMissionCenterItem(missionCenterItemQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @PostMapping(value = "/feedback")
    public Result<Integer> feedback(@Valid @RequestBody MissionCenterFeedBackDTO missionCenterFeedBackDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验
        return Result.success(
                missionCenterItemService.feedback(missionCenterFeedBackDTO, tokenUser)
        );
    }

    @Override
    @PostMapping("/exportReceivePath")
    public ResponseEntity<Resource> exportReceivePath(@RequestBody MissionCenterItemQuery missionCenterItemQuery) {
        TokenUser tokenUser = UserContext.getUser();
        String excelFilePath = missionCenterItemService.exportReceivePath(missionCenterItemQuery, tokenUser);
        // 编码文件名，防止乱码
        String filenames = new String(FilenameUtils.getName(excelFilePath)
                .getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=\"" + filenames + "\"")
                .header(HttpHeaders.CONTENT_TYPE, "application/vnd.ms-excel")
                .body(new FileSystemResource(excelFilePath));
    }

}

